package com.fw.web.controller.system;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fw.annotation.Log;
import com.fw.common.Builder;
import com.fw.core.controller.BaseController;
import com.fw.core.domain.AjaxResult;
import com.fw.core.page.TableDataInfo;
import com.fw.enums.BusinessType;
import com.fw.system.admin.domain.TbCityCode;
import com.fw.system.admin.domain.TbUserCityJoin;
import com.fw.system.admin.domain.vo.CountCityUserVo;
import com.fw.system.admin.service.ITbCityCodeService;
import com.fw.system.admin.service.ITbUserCityJoinService;
import com.fw.utils.DateUtils;
import com.fw.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户和战队中间Controller
 *
 * @author yanwei
 * @date 2023-04-04
 */
@Api(tags = "user_city_join")
@RestController
@RequestMapping("/admin/user_city_join")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class TbUserCityJoinController extends BaseController {
    @Autowired
    private ITbUserCityJoinService tbUserCityJoinService;
    @Autowired
    private ITbCityCodeService cityCodeService;

    /**
     * 查询用户和战队中间列表
     */
    @PreAuthorize("@ss.hasPermi('admin:user_city_join:list')")
    @GetMapping("/list")
    @ApiOperation("查询用户和战队中间列表")
    public TableDataInfo list(TbUserCityJoin tbUserCityJoin) {
        startPage();
        List<TbUserCityJoin> list = tbUserCityJoinService.selectTbUserCityJoinList(tbUserCityJoin);
        return getDataTable(list);
    }

    /**
     * 导出用户和战队中间列表
     */
    @PreAuthorize("@ss.hasPermi('admin:user_city_join:export')")
    @Log(title = "用户和战队中间", businessType = BusinessType.EXPORT)
    @ApiOperation("导出用户和战队中间列表")
    @GetMapping("/export")
    public AjaxResult export(TbUserCityJoin tbUserCityJoin) {
        List<TbUserCityJoin> list = tbUserCityJoinService.selectTbUserCityJoinList(tbUserCityJoin);
        ExcelUtil<TbUserCityJoin> util = new ExcelUtil<TbUserCityJoin>(TbUserCityJoin.class);
        return util.exportExcel(list, "user_city_join");
    }

    /**
     * 获取用户和战队中间详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:user_city_join:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取用户和战队中间详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(tbUserCityJoinService.selectTbUserCityJoinById(id));
    }

    /**
     * 新增用户和战队中间
     */
    @PreAuthorize("@ss.hasPermi('admin:user_city_join:add')")
    @Log(title = "用户和战队中间", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增用户和战队中间")
    public AjaxResult add(@RequestBody TbUserCityJoin tbUserCityJoin) {
        return toAjax(tbUserCityJoinService.insertTbUserCityJoin(tbUserCityJoin));
    }

    /**
     * 修改用户和战队中间
     */
    @PreAuthorize("@ss.hasPermi('admin:user_city_join:edit')")
    @Log(title = "用户和战队中间", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改用户和战队中间")
    public AjaxResult edit(@RequestBody TbUserCityJoin tbUserCityJoin) {
        return toAjax(tbUserCityJoinService.updateTbUserCityJoin(tbUserCityJoin));
    }

    /**
     * 删除用户和战队中间
     */
    @PreAuthorize("@ss.hasPermi('admin:user_city_join:remove')")
    @Log(title = "用户和战队中间", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除用户和战队中间")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tbUserCityJoinService.deleteTbUserCityJoinByIds(ids));
    }


    @PostMapping("/countCityUserNumByTime")
    @PreAuthorize("@ss.hasPermi('admin:user_city_join:countCityUserNumByTime')")
    @Log(title = "用户和战队中间", businessType = BusinessType.DELETE)
    @ApiOperation("统计战队新增人数，根据时间查询")
    public AjaxResult countCityUserNumByTime(@RequestBody JSONObject jsonObject) {
        Date date = jsonObject.getDate("date");
        Date endTime = jsonObject.getDate("endTime");

        List<CountCityUserVo> collect = cityCodeService.list(Wrappers.<TbCityCode>lambdaQuery().ge(TbCityCode::getSortCode, 1)).stream()
                .map(map -> {
                    CountCityUserVo build = Builder.of(CountCityUserVo::new).build();
                    build.setCityName(map.getCityName());
                    LambdaQueryWrapper<TbUserCityJoin> queryWrapper = new LambdaQueryWrapper<TbUserCityJoin>().eq(TbUserCityJoin::getCityCode, map.getId());
                    if (date != null && endTime != null) {
                        queryWrapper.between(TbUserCityJoin::getCreateTime, date, endTime);
                        build.setCreateTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,date) + "-" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,endTime));

                    }else{
                        build.setCreateTime("全部");

                    }
                    int count = tbUserCityJoinService.count(queryWrapper);
                    build.setUserCount(count);
                    return build;
                }).collect(Collectors.toList());

        return AjaxResult.success(collect);
    }

    @PreAuthorize("@ss.hasPermi('admin:user_city_join:exportCountCityUserNum')")
    @Log(title = "导出战队新增人数", businessType = BusinessType.EXPORT)
    @ApiOperation("导出战队新增人数")
    @PostMapping("/exportNew")
    public AjaxResult exportCountCityUserNum(@RequestBody JSONObject jsonObject){
        Date date = jsonObject.getDate("date");
        Date endTime = jsonObject.getDate("endTime");
        List<CountCityUserVo> collect = cityCodeService.list(Wrappers.<TbCityCode>lambdaQuery().ge(TbCityCode::getSortCode, 1)).stream()
                .map(map -> {
                    CountCityUserVo build = Builder.of(CountCityUserVo::new).build();
                    build.setCityName(map.getCityName());
                    LambdaQueryWrapper<TbUserCityJoin> queryWrapper = new LambdaQueryWrapper<TbUserCityJoin>().eq(TbUserCityJoin::getCityCode, map.getId());
                    if (date != null && endTime != null) {
                        queryWrapper.between(TbUserCityJoin::getCreateTime, date, endTime);
                        build.setCreateTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,date) + "-" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,endTime));
                    }else{
                        build.setCreateTime("全部");
                    }
                    int count = tbUserCityJoinService.count(queryWrapper);
                    build.setUserCount(count);
                    return build;
                }).collect(Collectors.toList());
        ExcelUtil<CountCityUserVo> util = new ExcelUtil<CountCityUserVo>(CountCityUserVo.class);
        return util.exportExcel(collect, "战队新增人数");
    }
}
